package com.lonty.intelligence.controller;

import com.lonty.intelligence.api.Result;
import com.lonty.intelligence.api.ResultCode;
import com.lonty.intelligence.request.CommunityNotificationList;
import com.lonty.intelligence.service.CommunityNotificationService;
import com.lonty.intelligence.vo.CommunityNotificationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author 陈璇
 * @Date 2020/5/28 14:50
 * @Version 1.0
 */
@RestController
@RequestMapping("app/communityNotification")
@Validated
@Api(tags = "社区通知接口", produces = "application/json")
public class CommunityNotificationController {

    @Resource
    private CommunityNotificationService communityNotificationService;

    @ApiOperation(value = "保存社区通知", httpMethod = "POST")
    @PostMapping("saveCommunityNotification")
    public Result saveCommunityNotification(@ModelAttribute CommunityNotificationVo communityNotificationVo){
        if(communityNotificationService.saveCommunityNotification(communityNotificationVo)){
            return Result.builder().build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

    @ApiOperation(value = "统计用户未读", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userInfoId", value = "用户ID",dataType = "string", paramType = "query", required = true)
    })
    @GetMapping("countUnread")
    public Result countUnread(String userInfoId){
        return Result.builder()
                .data(communityNotificationService.countUnread(userInfoId))
                .build();
    }

    @ApiOperation(value = "获取用户所有社区通知", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userInfoId", value = "用户ID",dataType = "string", paramType = "query", required = true)
    })
    @GetMapping("getCommunityNotificationList")
    public Result getCommunityNotificationList(String userInfoId){
        return Result.builder()
                .data(communityNotificationService.getCommunityNotificationList(userInfoId))
                .build();
    }

    @ApiOperation(value = "获取社区通知详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userInfoId", value = "用户ID",dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "communityNotificationId", value = "通知ID",dataType = "string", paramType = "query", required = true),
    })
    @GetMapping("getCommunityNotification")
    public Result getCommunityNotification(String userInfoId, String communityNotificationId){
        CommunityNotificationList communityNotificationList = communityNotificationService.getCommunityNotification(userInfoId, communityNotificationId);
        if(communityNotificationList != null){
            return Result.builder().data(communityNotificationList).build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

    @ApiOperation(value = "删除通知", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "通知ID",dataType = "string", paramType = "query", required = true)
    })
    @PostMapping("deleteNotification")
    public Result deleteNotification(String id){
        if(communityNotificationService.deleteNotification(id)){
            return Result.builder().build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

}
